package com.quanyan.ic.datacenter.mq.service.impl;

import com.github.ltsopensource.core.commons.utils.DateUtils;
import com.quanyan.ic.common.model.OrderMessage;
import com.quanyan.ic.datacenter.constant.Constants;
import com.quanyan.ic.datacenter.dao.*;
import com.quanyan.ic.datacenter.mapper.place.entity.TbBizPlace;
import com.quanyan.ic.datacenter.model.AlarmDayReportParam;
import com.quanyan.ic.datacenter.model.PlaceUnitInfo;
import com.quanyan.ic.datacenter.mq.service.ConsumerOrderMqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by jianzhou on 2017/8/28.
 */
@Service
public class ConsumerOrderMqServiceImpl implements ConsumerOrderMqService {

    @Autowired
    private PlaceOrderDao placeOrderDao;

    @Autowired
    private PlaceDao placeDao;

    @Autowired
    private AlarmDayReportOfCityDao alarmDayReportOfCityDao;
    @Autowired
    private AlarmDayReportOfPlaceDao alarmDayReportOfPlaceDao;
    @Autowired
    private AlarmTimePointReportOfCityDao alarmTimePointReportOfCityDao;
    @Autowired
    private AlarmTimePointReportOfPlaceDao alarmTimePointReportOfPlaceDao;

    @Override
    @Transactional
    public void consumerOrderMsg(OrderMessage orderMessage,PlaceUnitInfo unitInfo){
        Date date = DateUtils.convert(orderMessage.getStartTime());//消费日期/异常日期
        Integer placeId = orderMessage.getPlaceId();
        TbBizPlace tbBizPlace = placeDao.findTbBizPlace(placeId);
        String placeName = unitInfo.getPlaceName();
        Integer cityId = unitInfo.getCityId() ;
        String cityName = unitInfo.getCityName();
        long between=( orderMessage.getEndTime() - orderMessage.getStartTime())/1000;//除以1000是为了转换成秒
        Long min =between/60;;
        if ( tbBizPlace != null ){
            cityId = tbBizPlace.getCityId();
            placeName = tbBizPlace.getName();
        }


        AlarmDayReportParam alarmDayReportParam = new AlarmDayReportParam();
        alarmDayReportParam.setCityId(cityId);
        alarmDayReportParam.setCityName(cityName);
        alarmDayReportParam.setDate(date);
        alarmDayReportParam.setType(Constants.AlarmsType.order);
        alarmDayReportParam.setMin(min.intValue());
        alarmDayReportParam.setPlaceId(placeId);
        alarmDayReportParam.setPlaceName(placeName);
        alarmDayReportParam.setStartTimeInt(date.getHours());


        alarmDayReportOfCityDao.consumerOrder(alarmDayReportParam);
        alarmDayReportOfPlaceDao.consumerOrder(alarmDayReportParam);
        alarmTimePointReportOfCityDao.insertAlarmTimePointCity(alarmDayReportParam);
        alarmTimePointReportOfPlaceDao.insertAlarmTimePointPlace(alarmDayReportParam);


    }
}
